1662M - Bottle Arrangements - CodeForces Solution


constructive algorithms

Please click on ads to support us..

Python Code:

t = int(input())
for _ in range(t):
    n, m = map(int, input().split())
    critics = []
    maxR = 0
    maxW = 0
    for _ in range(m):
        r, w = map(int, input().split())
        maxR = max(maxR, r)
        maxW = max(maxW, w)

    if maxR + maxW > n:
        print("IMPOSSIBLE")
    else:
        print("W" * maxW + "R" * (n-maxW))

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define mmm                   1000000007
#define mm                    998244353ll
#define pb                    push_back
#define pk                    pop_back
#define mp                   make_pair
#define alt(x)                x.begin(), x.end()
#define cy                  "YES"
#define cn                  "NO"
using namespace std;
ll N=1e7+1;
vector<ll> hp(100001);
/*void sieve(ll n)
{
 vector<ll> c(n+1,1);
    for(ll i=2;i<=n;i++)
    {
        if(c[i])
        {
             hp[i]=i;
            for(ll j=2*i;j<=n;j+=i)
            {
                c[j]=0;
 
                    hp[j]=i;
            }}}} */
bool isPrime(int n)
{
    if(n==2)return true;
    if(n<=1)return false;
    if(n<=3)return true;
    if(n%2==0 || n%3==0)return false;
    for(int i=5;i*i<=n;i=i+6)
    if(n%i==0 || n%(i+2)==0)return false;
    return true;
}
unsigned long long power(unsigned long long x,
                         ll y, ll p)
{
    unsigned long long res = 1;
 
    x = x % p;
    while (y > 0)
    {
 
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res * x) % p;
 
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
 
// Returns n^(-1) mod p
unsigned long long modInverse(unsigned long long n,
                              int p)
{
    return power(n, p - 2, p);
}
 
vector<ll> fac;
void initfact(ll n)
{
    fac.resize(n + 1);
    fac[0] = 1;
    for (ll i = 1; i <= n; i++)
        fac[i] = (fac[i - 1] * i) % mmm;
}
unsigned long long nCrModPFermat(unsigned long long n,
                                 int r, int p)
{
    // If n<r, then nCr should return 0
    if (n < r)
        return 0;
    // Base case
    if (n == 0 && r == 0)
        return 1;
    if (n == 0)
        return 0;
    if (r == 0)
        return 1;
 
    // Fill factorial array so that we
    // can find all factorial of r, n
    // and n-r
 
    return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
 
ll countSetBits(ll n)
{
    ll count = 0;
    while (n)
    {
        count += n & 1;
        n >>= 1;
    }
    return count;
}
void banao_be(){
     ll n,m;
     cin>>n>>m;
     vector<pair<ll,ll>>v(m);
     for(ll i = 0;i<m;i++){
        cin>>v[i].first>>v[i].second;
     }
     string ans = "";
     ll r = INT_MIN , w = INT_MIN;
     for(ll i = 0;i<m;i++){
        r = max(v[i].first,r);
        w = max(v[i].second,w);
     }
     if(r+w>n)cout<<"IMPOSSIBLE"<<endl;
     else {
        for(ll i = 1;i<=w;i++)ans.pb('W');
        for(ll i = 1;i<=r;i++)ans.pb('R');
        for(ll i = ans.size();i<n;i++){
            ans.pb('W');
        }
        cout<<ans<<endl;
     }
}
int32_t main ()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
   // sieve(100001);
    while (t--){
        banao_be();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion
1657B - XY Sequence
1009A - Game Shopping
1657A - Integer Moves
230B - T-primes
630A - Again Twenty Five
1234D - Distinct Characters Queries
1183A - Nearest Interesting Number
1009E - Intercity Travelling
1637B - MEX and Array
224A - Parallelepiped
964A - Splits
1615A - Closing The Gap
4C - Registration System
1321A - Contest for Robots
1451A - Subtract or Divide
1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)
1579A - Casimir's String Solitaire
287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers
1624A - Plus One on the Subset